/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package as24gc.db;

import as24gc.db.AS24gcHibernateUtil;
import as24gc.db.Dealers;
import as24gc.db.Vehicles;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author nico.vassallo
 */
public class DBIntegrationFunction {

    public static final short UPDATED = 1;
    public static final short NOT_UPDATED = 0;
    
    
    public List getVehiclesByDealerId(int dealerId) {

        Session session = AS24gcHibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        System.out.println("dealerId: " + dealerId);
        Query q = session.createQuery("from Vehicles where dealer_id = ? and updated = ?").setInteger(0, dealerId).setInteger(1, 1);
        List vList = null;
        if (!q.list().isEmpty()) {
            vList =  q.list();
        }

        for (int i=0;i<vList.size(); i++)
            System.out.println("Vehicle: " + (Vehicles) vList.get(i));

        session.close();
        return vList;
    }
    
    
    

    public Vehicles getVehiclesById(int id) {

        Session session = AS24gcHibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Vehicles v = (Vehicles) session.get(Vehicles.class, id);
        System.out.println("Vehicle: " + v);

        session.close();
        return v;
    }

    public Vehicles getVehiclesByNidAndDealerId(int nid, int dealerId) {

        Session session = AS24gcHibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        System.out.println("nid" + nid + " dealerId: " + dealerId);
        Query q = session.createQuery("from Vehicles where nid = ? and dealer_id = ?").setInteger(0, nid).setInteger(1, dealerId);
        Vehicles v = null;
        if (!q.list().isEmpty()) {
            v = (Vehicles) q.list().get(0);
        }

 
        System.out.println("Vehicle: " + v);

        session.close();
        return v;
    }

    public int storeVehicles(Vehicles v) {

        Session session = AS24gcHibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Serializable saved = session.save(v);
        System.out.println("stored vehicles." + saved);
        session.getTransaction().commit();
        System.out.println("commit Transaction.");
        return Integer.parseInt("" + saved);
    }

    public void updateVehicles(Vehicles v) {
        System.out.println("updateVehicles...");
        Session session = AS24gcHibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.update(v);
        session.getTransaction().commit();
        System.out.println("commit Transaction.");
    }
    
    public void deleteVehicles(Vehicles v) {
        System.out.println("deleteVehicles...");
        Session session = AS24gcHibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.delete(v);
        session.getTransaction().commit();
        System.out.println("commit Transaction.");
    }

    public Dealers getDealers(int dealerId) {
        System.out.println("input dealerId: " + dealerId);
        Session session = AS24gcHibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Dealers d = (Dealers) session.get(Dealers.class, dealerId);
        System.out.println("Dealer: " + d.toString());

        session.close();
        return d;
    }
}
